package edu.zsl.service.impl;

import edu.zsl.mapper.AccountMapper;
import edu.zsl.mapper.StudentMapper;
import edu.zsl.pojo.Account;
import edu.zsl.pojo.Student;
import edu.zsl.service.AccountStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class AccountStudentServiceImpl implements AccountStudentService {
    
    private final AccountMapper accountMapper;
    private final StudentMapper studentMapper;
    @Autowired
    public AccountStudentServiceImpl(AccountMapper accountMapper, StudentMapper studentMapper) {
        this.accountMapper = accountMapper;
        this.studentMapper = studentMapper;
    }
    @Override
    @Transactional
    public void addStudentAccount(Student student) {
        LocalDateTime now = LocalDateTime.now();
        // 在数据库中创建Account
        Account account = new Account();
        account.setAccount(student.getId());// 学号
        account.setPassword("12346"); // 默认密码
        account.setRole(1); // 默认角色
        account.setCreateBy(1);
        account.setCreateTime(now);
        accountMapper.insertAccount(account);
        // 在数据库中创建Student
        student.setCreateBy(1);
        student.setCreateTime(now);
        studentMapper.insertStudent(student);
    }
    
    @Override
    @Transactional
    public void deleteStudentAccount(String account) {
        // 在数据库中删除student
        studentMapper.deleteById(account);
        // 在数据库中删除account
        accountMapper.deleteByAccount(account);
    }
}
